package com.ybl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ybl.common.CustomException;
import com.ybl.dto.SetmealDto;
import com.ybl.entity.Setmeal;
import com.ybl.entity.SetmealDish;
import com.ybl.mapper.SetmealMapper;
import com.ybl.service.SetmealDishService;
import com.ybl.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增套餐功能，同时需要保存套餐和菜品的关联关系
     */
    @Override
    @Transactional
    public void saveWithFlavor(SetmealDto setmealDto) {
        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().peek(
                (item -> item.setSetmealId(setmealDto.getId()))
        ).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 删除套餐，同时需要删除套餐和菜品的关联数据
     */
    @Override
    @Transactional
    public void getByIdDeleteWithDish(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId, ids);
        queryWrapper.eq(Setmeal::getStatus, 1);
        int count = this.count(queryWrapper);
        if (count > 0) {
            //如果不能删除，抛出一个业务异常
            throw new CustomException("套餐正在售卖中，不能删除");
        }
        this.removeByIds(ids);
        LambdaQueryWrapper<SetmealDish> SqueryWrapper = new LambdaQueryWrapper<>();
        for (Long id : ids) {
            SqueryWrapper.eq(SetmealDish::getDishId, id);
            SqueryWrapper.or();
        }
        setmealDishService.remove(SqueryWrapper);
    }

    /**
     * 修改商品状态
     * 1：启用
     * 0：停用
     */
    @Override
    @Transactional
    public void getByIdUpdaterState(Byte state, List<Long> ids) {
        //批量更改,只有状态一致的菜品才可以向下，否则抛出业务异常
        if (ids.size() > 1) {
            LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(Setmeal::getId, ids);
            byte b;
            if (state == 1) {
                b = 0;
            } else {
                b = 1;
            }
            queryWrapper.eq(Setmeal::getStatus, b);
            int count = this.count(queryWrapper);
            if (!(count == ids.size())) {
                throw new CustomException("状态不一致，操作失败");
            }
        }
        //更改单个，批量经过判断后来此更改
        LambdaQueryWrapper<Setmeal> SQW = new LambdaQueryWrapper<>();
        for (Long id : ids) {
            SQW.eq(Setmeal::getId, id);
            SQW.or();
        }
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(Integer.valueOf(state));
        this.update(setmeal, SQW);
    }

    /**
     * 根据id查套餐及相关的菜品信息
     */
    @Override
    public SetmealDto getByIdWithDish(Long id) {
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    /**
     * 套餐的修改功能
     */
    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
        //修改套餐表
        this.updateById(setmealDto);
        //修改关联菜品表
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(queryWrapper);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().peek(
                (i) -> i.setSetmealId(setmealDto.getId())
        ).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

}
